package com.zrx.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zrx.constants.SystemConstants;
import com.zrx.domain.ResponseResult;
import com.zrx.domain.entity.Comment;
import com.zrx.domain.vo.CommentVo;
import com.zrx.domain.vo.PageVo;
import com.zrx.enums.AppHttpCodeEnum;
import com.zrx.exception.SystemException;
import com.zrx.mapper.CommentMapper;
import com.zrx.service.CommentService;
import com.zrx.service.UserService;
import com.zrx.utils.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 评论表(Comment)表服务实现类
 *
 * @author makejava
 * @since 2022-09-19 14:18:16
 */
@Service("commentService")
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    @Autowired
    private UserService userService;

    /**
     *查询文章评论
     *
     * @param commentType
     * @param articleId
     * @param pageNum
     * @param pagrSize
     * @return
     */
    @Override
    public ResponseResult commentList(String commentType, Long articleId, Integer pageNum, Integer pagrSize) {
        //查询对应文章的跟评论


        LambdaQueryWrapper<Comment> queryWrapper =  new LambdaQueryWrapper<>();
        //对 articleId 进行判断    当commentType  为0 才进行判断
        queryWrapper.eq(SystemConstants.ARTICLE_COMMENTS.equals(commentType),Comment::getArticleId,articleId);
        //根评论 rootId 为-1
        queryWrapper.eq(Comment::getRootId, SystemConstants.ROOT_COMMENTS_NORMAL);

        //评论类型
        queryWrapper.eq(Comment::getType,commentType);

        //分页查询
        Page<Comment> page = new Page<>(pageNum,pagrSize);
        page(page,queryWrapper);

        List<CommentVo> commentVoList = toCommentVoList(page.getRecords());


        //查询所有跟评论对应的子评论 并赋值对应属性

        commentVoList.forEach(commentVo -> {
            //查询对应的子评论
            List<CommentVo>  children =   getChildren(commentVo.getId());
            //赋值
            commentVo.setChildren(children);

        });

        return ResponseResult.okResult(new PageVo(commentVoList,page.getTotal()));
    }


    /**
     *添加文章评论
     * @param comment
     * @return
     */
    @Override
    public ResponseResult addComment(Comment comment) {
        //评论内容不能为空
        if (!StringUtils.hasText(comment.getContent())){
            throw new SystemException(AppHttpCodeEnum.CONTENT_NOT_NULL);
        }
        save(comment);

        return ResponseResult.okResult();
    }







    /**
     * 根据根评论的 id 查询对应的子评论的集合
     * @param id 根评论的id
     * @return
     */
    private List<CommentVo> getChildren(Long id) {

        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Comment::getRootId,id);
        queryWrapper.orderByDesc(Comment::getCreateTime);

        List<Comment> comments = list(queryWrapper);

        List<CommentVo> commentVos = toCommentVoList(comments);

        return commentVos;

    }

    /**
     * 给查询的评论赋值
     * @param list
     * @return
     */
    private List<CommentVo> toCommentVoList(List<Comment> list){
        List<CommentVo> commentVoList = BeanCopyUtils.copyBeanList(list,CommentVo.class);
        //遍历vo集合

        commentVoList.forEach(commentVo ->{

            //头像
            String avatar = userService.getById(commentVo.getCreateBy()).getAvatar();
            commentVo.setAvatar(avatar);
            // createBy  查询当前评论用户的名称并赋值
            String nickName = userService.getById(commentVo.getCreateBy()).getNickName();
            commentVo.setUsername(nickName);
            //toCommentUserId 查询被评论用户的名称并赋值
            //如果 toCommentUserId 不为-1
            if (commentVo.getToCommentUserId()!= -1){
                String ToCommentUserName = userService.getById(commentVo.getToCommentUserId()).getNickName();
                commentVo.setToCommentUserName(ToCommentUserName);
            }
        });

        return commentVoList;

    }
}

